//
// PayOffScene.cpp
//  airfight
//
//  Created by aaron on 12-11-28.
//  Copyright (c) 2012年 moonriver. All rights reserved.
//

#include "PayOffLayer.h"
#include "MenuLayer.h"

PayOffLayer::PayOffLayer()
{
    bonusDictionary = CCDictionary::create();
}

PayOffLayer::~PayOffLayer()
{
    
}

bool PayOffLayer::init(CCDictionary* p_params)
{
    CCLayer::init();
    bonusDictionary = CCDictionary::create(p_params);
    
    m_SpriteFrameCache = CCSpriteFrameCache::sharedSpriteFrameCache();
    m_SpriteFrameCache->addSpriteFramesWithFile("airfight.plist","airfight.png");
    m_SpriteBatch = CCSpriteBatchNode::create("airfight.png");
    
    CCSprite *back = CCSprite::create();
    back->initWithSpriteFrameName("background_end.png");
    back->setPosition(ccp(160, 240));
    addChild(back);
    
    CCSprite *backBtn = CCSprite::create();
    backBtn->initWithSpriteFrameName("button.png");
    CCSprite *backBtnClicked = CCSprite::create();
    backBtnClicked->initWithSpriteFrameName("button.png");
    backBtnClicked->setScale(0.95);
    CCMenuItemSprite *backMenuItem = CCMenuItemSprite::itemWithNormalSprite(backBtn, backBtnClicked, backBtn, this, menu_selector(PayOffLayer::clickBack));
    backMenuItem->setPosition(ccp(220, 120));
    CCLabelTTF *labelBack = CCLabelTTF::labelWithString("返回", CCSize(50, 30), kCCTextAlignmentLeft, kCCVerticalTextAlignmentCenter, "", 15);
    labelBack->setPosition(ccp(230, 122));
    
    CCMenu *menu = CCMenu::menuWithItems(backMenuItem, NULL);
    menu->setPosition(CCPointZero);
    addChild(menu);
    addChild(labelBack);
        
    return true;
}

void PayOffLayer::clickBack()
{
    CCScene *pScene = CCScene::create();
    CCLayer *pLayer = MenuLayer::create();
    pScene->addChild(pLayer);
    CCDirector::sharedDirector()->replaceScene(pScene);
}

void PayOffLayer::setType(PayOff_type type)
{
    if (PAYOFF_PASS == type)
    {
        CCSprite *title = CCSprite::create();
        title->initWithSpriteFrameName("win.png");
        title->setPosition(ccp(160, 400));
        addChild(title);
    }
    else if (PAYOFF_DIE == type)
    {
        CCSprite *title = CCSprite::create();
        title->initWithSpriteFrameName("gameover.png");
        title->setPosition(ccp(160, 400));
        addChild(title);
    }
}

void PayOffLayer::onEnter()
{
    int bonusCount = bonusDictionary->count();
    CCLayer::onEnter();
    BonusPrototypes *bonusPrototypes = BonusPrototypes::getInstance();
    CCSize s = CCDirector::sharedDirector()->getWinSize();
    
    CCDictElement* bonusElement = NULL;
    CCDICT_FOREACH(bonusDictionary, bonusElement)
    {
        int bonusid = bonusElement->getIntKey();
        CCSprite *bonusSprite_star = CCSprite::create();
        bonusSprite_star->initWithSpriteFrameName((bonusPrototypes->getBonusByID(bonusid))->getRes().c_str());
        bonusSprite_star->setPosition(CCPointMake(120, 220+(40*--bonusCount)));
        addChild(bonusSprite_star);
        CCString *value  = (CCString*)bonusElement->getObject();
        CCLabelTTF *value_label = CCLabelTTF::create(value->getCString(), "Marker Felt", 16);
        value_label->setPosition(CCPointMake( bonusSprite_star->getPosition().x+60,bonusSprite_star->getPosition().y));
        addChild(value_label);
    }
}

void PayOffLayer::onExit()
{
    CCLayer::onExit();
}
